#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "llist.h"

#define NAMESIZE (80)
struct score_st
{
    int id;
    char name[NAMESIZE];
    int math;
    int chinses;
};

static void printf_s(const void *record)
{
    const struct score_st *r = record;
    printf("%d %s %d %d\n", r->id,r->name,r->math,r->chinses);
}

static int id_cmp(const void *key,const void *record)
{
    const int *k = key;
    const struct score_st *r = record;
    return (*k - r->id);
}
static int name_cmp(const void *key,const void *record)
{
    const char *k = key;
    const struct score_st *r = record;
    return strcmp(k,r->name);
    //strcmp 在string.h 中，虽然main中不加，也可以编译通过（llist.h有定义）会是隐式，有告警
}

int main()
{
    LLIST *handler;
    struct score_st tmp;
    int i, ret;

    handler = llist_creat(sizeof(struct score_st));
    if (handler == NULL) {
        exit(-1);//退出当前进程
    }
    for (i = 0; i < 7; i++) {
        tmp.id = i;
        snprintf(tmp.name,NAMESIZE,"std%d",i);
        tmp.math = rand() % 100;
        tmp.chinses = rand() % 100;
        ret = llist_insert(handler,&tmp, LLIST_FORWARD);
        if (ret) {
            exit(1);
        }
    }

    llist_travel(handler,printf_s);
    printf("\n\n");
    int id = 30;
    char *name = "std6";
    //ret = llsit_delete(handler,&id,id_cmp);
    ret = llsit_delete(handler,name,name_cmp);
    if (ret != 0) {
        printf("llist_delete fail!\n");
    }
    llist_travel(handler,printf_s);

#if 0
    struct score *data;
    data = llist_find(handler,&id,id_cmp);
    if (data == NULL) {
        printf("Cannot find!\n");
    } else {
        printf_s(data);
    }
    llist_find(handler,&id,id_cmp);
#endif
    llist_destroy(handler);
    return 0;

}